package myTools;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author yhy
 * @version 1.0.0
 * @create 2024/11/2 18:00
 */
public class ThreadPoolTools {

    private ThreadPoolTools(){}

    private static ThreadPoolExecutor createExecutor() {
        // 机子的核数
        int processors = Runtime.getRuntime().availableProcessors();
        return new ThreadPoolExecutor(
                processors+1, // 核心线程个数 io:2n ,cpu: n+1  n:内核数据
                processors+1,
                0,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
    }

    private static final class ThreadPoolExecutorHolder {
        static final ThreadPoolExecutor threadPoolExecutor = createExecutor();
    }

    public static ThreadPoolExecutor getThreadPoolExecutor() {
        return ThreadPoolExecutorHolder.threadPoolExecutor;
    }
}
